/**
  * @desc add method res.error
*/

const ResponseError = require('../helper/ResponseError')

const addResponseError = (req, res, next) => {
  res.error = (errors) => {
    const responseErrors = [].concat(errors)
    if (responseErrors.length === 0) {
      responseErrors.push(new ResponseError.InternalServerError())
    }
    res.status(responseErrors[0].code)
    res.json({ errors: responseErrors })
  }
  return next()
}

module.exports = addResponseError
